
(defun f(x y)
  (/ y x))

(defun avdump(f x y)
  (/ (+ x (f x y))
     2.0))

(defun solve(f imp x)
  "求 f(x) = x 的解"
  (defun iter (guess)
    (if (close guess (funcall f guess x))
	guess
      (iter (imp guess))))

  (defun close (a b)
    (< (abs (- a b))
       0.001))
  (iter 1)
  )

(defun solve2(f imp x)
  (defun iter (guess )
    (if (close guess (funcall f guess))
	guess
      (iter (imp guess))))

  (defun close (a b)
    (< (abs (- a b))
       0.001))
  (iter 1)
  )

(defun fix-point(f start)
  "fix-point"
  (defvar elapase 0.001)
  (defun close (a b)
    (< (abs (- a b))
       elapase))
  (defun iter(old new)
    (if (close old new)
	new
      (iter new (funcall f new))))
  (iter start (funcall f start))
  )

(defun average(x y)
  (/ (+ x y) 2.0))

(average 1 2)

(defun average-dump(f)
  "平均阻尼"
  (lambda(x)
    (message "lambda===x:%d" x)
    (print f)
    (average x (funcall f x)))
  )

(print 'average-dump)

(defvar xxoo nil)
(setq xxoo (average-dump (lambda (x)
			   (message "IN LAMBDA x = %d" x)
			   x)))
(print xxoo)
(funcall xxoo 10)





;; (defun id(x) x)

;; (funcall (average-dump 'id) 3)

;; (defun func-1(k)
;;   (message "func-1===k :%d" k)
;;   (/ 2.0 k))


;; (funcall
;;  (average-dump (lambda(x)
;; 		 (message "x in lambda:%d" x)
;; 		 (/ 2.0 x))) 3)






;; (setq x1 (average-dump 'func-1))
;; (funcall x1 5)



;; (defun my-sqrt(n)
;;   "通过不动点求 n 的平方根"
;;   (fix-point
;;    (average-dump (lambda(x)
;; 		   (/ y x)))
;;    1))

;; (setq l1 (lambda(x)
;; 	   (message "x:%d" x)
;; 	   (* x x)))
;; (funcall l1 5)


;; (funcall (lambda(x)
;; 	    (message "XXX:%d" x)
;; 	    (* x x)) 8)


;; (defun dd (x)
;;   (* x x))

;; (dd 2)

;; (defun dd-sum (f a b)
;;   (if (> a b)
;;       0
;;     (+ (funcall f a)
;;        (dd-sum f (+ a 1) b)))
;;   )
;; (dd-sum (lambda(x)
;; 	  (* x x)) 3 5)

;; (dd-sum 'dd 3 5)
